package com.mydb;

import java.util.List;
import java.util.Map;

import org.dom4j.Document;

import com.mydb.xml.XMLBuilder;

public class MyDbObject {
	
	private String dbName;
	private Document dbDoc;
	
	public MyDbObject() {
		
	}

	public Document getDbDoc() {
		return dbDoc;
	}

	public void setDbDoc(Document dbDoc) {
		this.dbDoc = dbDoc;
	}
	
	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public int insert(String tableName, Map data) {
		int result = 0;
		XMLBuilder builder = XMLBuilder.getInstance();
		try {
			builder.addElement(dbName, tableName, data);
			result = 1;
		} catch (MyDbException e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}
	
	public int delete(String tableName, Map cond) {
		int result = 0;
		XMLBuilder builder = XMLBuilder.getInstance();
		try {
			result = builder.removeElement(dbName, tableName, cond);
		} catch (MyDbException e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}
	
	public int update(String tableName, Map updateData, Map cond) {
		int result = 0;
		XMLBuilder builder = XMLBuilder.getInstance();
		try {
			result = builder.updateElement(dbName, tableName, updateData, cond);
		} catch (MyDbException e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}
	
	public List select(String tableName, Map cond) {
		XMLBuilder builder = XMLBuilder.getInstance();
		try {
			return builder.getElementList(dbName, tableName, cond);
		} catch (MyDbException e) {
			e.printStackTrace();
			return null;
		}
	}
}
